package com.softhinker.ke24.unit3.ui;

import java.awt.event.MouseListener;
import java.util.List;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;

import com.softhinker.ke24.unit3.listener.ResultListener;
import com.softhinker.ke24.unit3.model.Stock;

public class ResultPanel extends JPanel implements ResultListener {

	private static final long serialVersionUID = 7964003966819545138L;

	private JTable dataTable;
    private DefaultTableModel tableModel;
    private Object[] columns = new Object[]{"Stock", "Score"};
    
	public ResultPanel(MouseListener mouseListener){
		dataTable = new JTable();
		
		tableModel = new DefaultTableModel();
		dataTable.setModel(tableModel);
        dataTable.setFillsViewportHeight(true);
        dataTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        dataTable.addMouseListener(mouseListener);
        
        JScrollPane scrollPane = new JScrollPane(dataTable);
        this.add(scrollPane);
	}

	@Override
	public void publishResult(List<Stock> stocks) {
		Object[][] data = new Object[stocks.size()][2];
		for(int i=0;i<stocks.size();i++){
			Stock stock = stocks.get(i);
			data[i][0] = stock;
			data[i][1] = stock.getScore();
		}
		this.tableModel.setDataVector(data, columns);
		tableModel.fireTableDataChanged();
		this.repaint();
	}

}
